package selection;

import java.util.ArrayList;
import java.util.Random;

import model.Chromosome;

public class Tournament implements Selection {

	Random rand= new Random();
	@Override
	public Chromosome[] selectPatents(ArrayList<Chromosome> population) {
		Chromosome parent1=selectParent(population);
		Chromosome parent2=selectParent(population);
		return new Chromosome[] {parent1,parent2};
	}
	Chromosome selectParent(ArrayList<Chromosome> population){
		int index1=rand.nextInt(population.size());
		int index2=rand.nextInt(population.size());
		double flag=rand.nextDouble();
		if(flag>0.75){
			if(population.get(index1).getFitness()<population.get(index2).getFitness())
				return population.get(index1);
			else
				return population.get(index2);
		}
		else{
			if(population.get(index1).getFitness()>=population.get(index2).getFitness())
				return population.get(index1);
			else
				return population.get(index2);
			
		}
		
	}
	@Override
	public String toString() {
		return "Tournament";
	}

}
